package com.zsinfo.guoss.supplier.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.example.netcenter.Event.Response;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.example.netcenter.bean.NewProductBean;
import com.example.netcenter.bean.ResultsList;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;
import com.zsinfo.guoss.supplier.utils.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class NewProductActivity extends BaseActivity {

    @BindView(R.id.xrv)
    XRecyclerView xrv;
    @BindView(R.id.ll_empty)
    LinearLayout ll_empty;

    private CommonAdapter productAdapter;
    private ResultsList<NewProductBean> resultsLists;  //服务器返回的数据
    private List<NewProductBean> list = new ArrayList<>();  //列表数据
    private int pageNo = 1;
    private int pageSize = 10;
    private boolean isLast = false; //是否是最后一条数据
    private boolean isRefresh = false, isLoadMore = false; //默认不刷新，不加载

    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_new_product;
    }

    @Override
    protected void initView() {

        setTitle("我的上新");
        setBackAndLeftTitle("").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(NewProductActivity.this, NewProductAddActivity.class), 1);
            }
        });

        setAdapter();
    }

    @Override
    protected void initNetData() {
        GetNetCenter().GetNetCenter(
                RetrofitBuilder.CreateApi().NEW_GOOD_LIST(
                        ConstantsCode.new_goods_list,
                        SharedPreferencesUtil.getID(),
                        pageNo + "", pageSize + ""), new Response() {
                    @Override
                    public void OK(Object data) {
                        Log.e("lixl", data.toString());
                        setDataView(data);
                    }

                    @Override
                    public void Fail(Throwable e) {
                        Log.e("lixl", e.getMessage());
                    }

                    @Override
                    public void complete() {
                        if (isRefresh) {
                            xrv.refreshComplete();
                        }
                        if (isLoadMore) {
                            xrv.loadMoreComplete();
                        }
                    }
                });
    }

    @Override
    protected void customDestroy() {

    }


    private void setAdapter() {
        xrv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        xrv.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xrv.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        xrv.setArrowImageView(R.mipmap.iconfont_downgrey);
        xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //清除数据
                list.clear();
                //更新适配器
                productAdapter.notifyDataSetChanged();
                //刷新操作
                pageNo = 1;
                //重新请求接口
                initNetData();
                //正在执行刷新
                isRefresh = true;
            }

            @Override
            public void onLoadMore() {
                //可以判断 isLast=true，表示已经没有更多数据了
                if (isLast) {
                    showToast("当前已经没有更多数据了");
                    xrv.loadMoreComplete(); //停止加载的动画效果
                    return;
                }
                //加载更多
                pageNo++;
                //请求加载接口
                initNetData();
                //正在执行加载
                isLoadMore = true;
            }
        });
        productAdapter = new CommonAdapter<NewProductBean>(this, R.layout.item_new_product, list) {
            @Override
            protected void convert(ViewHolder holder, final NewProductBean newProductBean, int position) {
                //数据展示
                holder.setText(R.id.product_time, newProductBean.getCreateTime());
                holder.setText(R.id.product_name, newProductBean.getGoodsName());
                //判断审核状态：0审核中，1审核成功，-1审核失败
                int status = newProductBean.getStatus();
                if (status == 1) {
                    holder.setText(R.id.product_state, "审核成功")
                            .setTextColorRes(R.id.product_state, R.color.green);
                } else if (status == 0) {
                    holder.setText(R.id.product_state, "审核中")
                            .setTextColorRes(R.id.product_state, R.color.red);
                } else if (status == -1) {
                    holder.setText(R.id.product_state, "审核失败")
                            .setTextColorRes(R.id.product_state, R.color.gray);
                }
            }
        };
        productAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                position--; //防止数组下标越界
                Intent i = new Intent(NewProductActivity.this, NewProductDetailActivity.class);
                i.putExtra("newGoodsId", list.get(position).getId());
                startActivity(i);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        xrv.setAdapter(productAdapter);
    }

    /**
     * 设置数据到view
     *
     * @param data
     */
    private void setDataView(Object data) {
        resultsLists = (ResultsList<NewProductBean>) data;


        //赋值
        isLast = resultsLists.getData().isIsLast();
        //判断一下，currentPageNo==1是表示第一次加载数据，如果第一次加载数据为空，就表示没有数据
        List<NewProductBean> datas = resultsLists.getData().getObjects();
        if (pageNo == 1) {
            if (datas == null || datas.size() == 0) {
                xrv.setVisibility(View.GONE);
                ll_empty.setVisibility(View.VISIBLE);
                return;
            } else {
                xrv.setVisibility(View.VISIBLE);
                ll_empty.setVisibility(View.GONE);
            }
        }
        //更新适配器
        list.addAll(datas);
        productAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 2) {
            NewProductBean newProductBean = (NewProductBean) data.getSerializableExtra("newProductBean");
            //最新添加的商品，放在第一个位置
            list.add(0, newProductBean);
            //第一次，新增商品之后，需要把RecyclerView显示出来
            if (xrv.getVisibility() == View.GONE) {
                xrv.setVisibility(View.VISIBLE);
                ll_empty.setVisibility(View.GONE);
            }
            //更新适配器
            productAdapter.notifyDataSetChanged();
        }
    }
}
